package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import client.CcrmApp;
import view.FinalAccOrRejGui;
import model.CustomerHistoryModel;
import model.Envelope;
/**
 * this class control on the final decistion 
 * @author liran
 *
 */
public class FinalAccOrRejController extends AbstractTransfer{
	
	/**
	 * salecon - go back to main menu
	 */
	private SalesManControllers salecon = null;
	/**
	 * parameter for this controller
	 */
	private FinalAccOrRejController currcon = null;
	/**
	 * class gui !!
	 */
	private FinalAccOrRejGui currgui = null;
	/**
	 * Campaign ID 
	 */
	private String CampaingId = "";
	/**
	 * current date
	 */
	private String CurrentDate = "";
	/**
	 * user ID
	 */
	private String userID = "";
	/**
	 * flag for algoritem
	 */
	private boolean LetsContinue = false;
	/**
	 * constructor
	 * @param s -> SalesManControllers
	 * @param f -> FinalAccOrRejGui
	 * @param Campid -> Campaign ID 
	 * @param cDate ->  current date
	 * @param Uid ->  user ID
	 */
	public FinalAccOrRejController(SalesManControllers s,FinalAccOrRejGui f,String Campid,String cDate,String Uid)
	{
		super();
		salecon = s;
		currcon = this ;
		currgui = f;
		CampaingId =Campid ;
		CurrentDate = cDate;
		userID = Uid; 
		
		currgui.addADDPREbtnAC(new PreAC());
		currgui.addFinishbtnAC(new FinishAC());
		
	}
	
	
	/**
	 *handle result from data base 
	 * @param prS -> list of permissions 
	 */
	public void hendleDBcombo(String prS)
	{
  		String[] q=prS.trim().split(",");
  		currgui.getpermisscomboBox().removeAllItems();
  		for (int i =0 ; i < q.length; i++)
  		{
  			currgui.setItemComboPermm(q[i]);
  		}

	}
	/**
	 * sever return list and this func insert to combo box
	 */
	public void hendleDBadd()
	{
		Envelope ev1 = new Envelope(userID,"take all prem");
		CcrmApp.clien.setCurrObj(currcon);
		sendToServer(ev1);
	}
	/**
	 * 
	 * ActionListener for add permission
	 *
	 */
	class Addpremmm implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			String s = new String();
			s = (String) currgui.getpermisscomboBox().getSelectedItem();
			System.out.println(s);
			s+=",";
			s+=userID;
			System.out.println(s);
			Envelope ev = new Envelope(s,"update the prem user");
			sendToServer(ev);

			
		}
		
	}
	/**
	 * this ActionListener for go back 
	 * @author liran
	 *
	 */
	class Gogobak implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			currgui.gobackPlzz();
		}
	}
	
	/**
	 * this class add permission panel
	 * @author liran
	 *
	 */
	class PreAC implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			currgui.PanelAddPermmision();
			currgui.addADDFromComboAC(new Addpremmm());
			currgui.adgoBackAC(new Gogobak());
			Envelope ev = new Envelope(userID,"take all prem");
			CcrmApp.clien.setCurrObj(currcon);
			sendToServer(ev);
		}
	}
	
	/**
	 * END SALE
	 * @author moshe
	 *
	 */
	class FinishAC implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			CustomerHistoryModel custhist = new CustomerHistoryModel("AA","AA",userID);
			String sss ;
			sss = " ";
			int QQQ = 0;
			int timeC = 0;
			int flag = 1;
			if ((currgui.getTimeCalltxt().equals(""))==false){
				timeC = Integer.parseInt(currgui.getTimeCalltxt());
			}
			
			if (currgui.getStatus() == 1){
			if ((currgui.getCommettxt().equals("write here"))==false){
				sss = currgui.getCommettxt();
				}
			
			else{
				flag=0;
			}
			}
			if (currgui.getStatus()==0){
			if ((currgui.getQuantitytxt().equals("0"))==false)
			{
				QQQ = Integer.parseInt((currgui.getQuantitytxt()));		
			}
			else{
				flag=0;
			}
			}
			
			if (flag==1){
			System.out.println("ohhh yeeee");
			custhist.SetHistory(CampaingId,QQQ ,CurrentDate, sss,timeC );
			System.out.println("ohhh yeeee");
			
			Envelope ev = new Envelope(custhist,"update customer history");
			sendToServer(ev);
			
			
			
			currgui.dispose();
			salecon.GetSalesManGui().setVisible(true);
			}
		}
		
	}
}
